package com.lry.crawlers.cartoon.pipeline;

import com.lry.crawlers.cartoon.entity.*;
import com.lry.crawlers.cartoon.service.CartoonChapterService;
import com.lry.crawlers.cartoon.service.CartoonContentService;
import com.lry.crawlers.cartoon.service.CartoonService;
import com.lry.crawlers.cartoon.service.CartoonUpdateService;
import com.lry.crawlers.message.entity.Message;
import com.lry.crawlers.message.service.MessageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class CartoonPipeline implements Pipeline {
    @Autowired
    CartoonService cartoonService;
    @Autowired
    CartoonChapterService cartoonChapterService;
    @Autowired
    CartoonContentService cartoonContentService;
    @Autowired
    CartoonUpdateService cartoonUpdateService;
    @Autowired
    MessageService messageService;




    public void process(ResultItems resultItems, Task task) {
        //获取数据
        Map<String,Object> map= resultItems.get("data");

        //判断数据是否不为空
        if(map!= null) {
            //判断数据类型
            if (map.get("key").equals("cartoonChapter")){
                cartoon(map);
                chapter(map);
            }else if(map.get("key").equals("chapter")){
                chapter(map);
            }else if(map.get("key").equals("content")){
                content(map);
            }


        }
    }
    //处理漫画搜索
    public void cartoon(Map<String,Object> map) {
        Cartoon cartoon = null;
        String title = (String)  map.get("title");
        String desc = (String)  map.get("desc");
        String author = (String)  map.get("author");
        String cartoonType =(String)  map.get("cartoonType");
        String cover = (String)  map.get("cover");
        String updateTime = (String)  map.get("updateTime");
        String cartoonId = (String) map.get("cartoonId");

        Integer id = (Integer) map.get("id");
        cartoon = new Cartoon();
        cartoon.setTitle(title.trim());
        cartoon.setAuthor(author.trim());
        cartoon.setCreationTime(new Date());
        cartoon.setDescs(desc.trim());
        cartoon.setCartoonId(id.toString()+cartoonId);
        cartoon.setCartoonType(cartoonType.trim());
        cartoon.setCover(cover);
        cartoon.setCartoonVariableId(id);
        cartoon.setUpdateTime(updateTime);

        try {
            //添加到mysql
            int i= cartoonService.setCartoon(cartoon);

            if(i==0){
                messageService.setMessage(new Message("mysql漫画搜索阶段入库失败,cartoonId:"+id.toString()+cartoonId,new Date()));
            }
        }catch (DuplicateKeyException e){
        }catch (Exception e){
            messageService.setMessage(new Message("漫画搜索阶段入库失败,cartoonId:"+id.toString()+cartoonId+" .Extends："+e,new Date()));
        }
    }


    //处理漫画章节
    public void chapter(Map<String,Object> map){


        CartoonChapter cartoonChapter = null;
        List<String> chapterTitle = (List<String>) map.get("chapterTitle");
        List<String> chapterId = (List<String>) map.get("chapterId");
        String cartoonId = (String) map.get("cartoonId");
        Integer xsize = (Integer) map.get("chapterNum");
        Integer id = (Integer) map.get("id");
        List<CartoonChapter> cartoonChapters = new ArrayList<>();
        String updateUrl = (String) map.get("updateUrl");
        updateUrl = "/"+updateUrl.split("/", 4)[3];
        for (int i =0;i<chapterTitle.size();i+=1) {
            cartoonChapter = new CartoonChapter();
            cartoonChapter.setChapterId(id.toString()+chapterId.get(i));
            cartoonChapter.setCartoonId(id.toString()+cartoonId);
            cartoonChapter.setTitle(chapterTitle.get(i).trim());
            cartoonChapter.setCreationTime(new Date());
            cartoonChapter.setCartoonVariableId(id);
            cartoonChapters.add(cartoonChapter);
        }

        try {
            //添加到mysql
            int i  = cartoonChapterService.setCartoonChapters(cartoonChapters);
            if(i==0){
                messageService.setMessage(new Message("mysql漫画章节阶段入库失败,cartoonId:"+id.toString()+cartoonId,new Date()));
            }else{
                int upi = 0;
                if(xsize!=null){
                    cartoonService.setupdateTime(new Cartoon(id.toString()+cartoonId,(String) map.get("updateTime")));


                    upi = cartoonUpdateService.setCartoonUpdateUp(new CartoonUpdate(id.toString()+cartoonId,updateUrl,xsize,id,(String) map.get("updateTime")));
                }else {
                    upi = cartoonUpdateService.setCartoonUpdate(new CartoonUpdate(id.toString()+cartoonId,updateUrl, cartoonChapters.size(),id,(String) map.get("updateTime")));
                }

                if(upi==0){
                    messageService.setMessage(new Message("漫画章节更新表入库失败,cartoonId:"+id.toString()+cartoonId,new Date()));
                }
            }

        }catch (DuplicateKeyException e){
        }catch (Exception e){
            messageService.setMessage(new Message("漫画章节阶段入库失败,cartoonId:"+id.toString()+cartoonId+" .Extends："+e,new Date()));
        }
    }

    //处理漫画内容
    public void content(Map<String,Object> map){
        List<String> content = (List<String>) map.get("content");
        String chapterId = (String) map.get("chapterId");
        String cartoonId = (String) map.get("cartoonId");

        Integer id = (Integer) map.get("id");
        CartoonVariable cartoonVariable = (CartoonVariable) map.get("cartoonVariable");
        CartoonContent cartoonContent = new CartoonContent();
        cartoonContent.setChapterId(id.toString()+chapterId);
        cartoonContent.setCartoonId(id.toString()+cartoonId);
        cartoonContent.setCreationTime(new Date());
        cartoonContent.setCartoonVariableId(id);
        if(content.size()==0){
            content.add("章节异常，请联系管理员处理");
        }
        List<String> contents = new ArrayList<String>();
        content.forEach(cont -> contents.add(cont.trim()));
        cartoonContent.setContent(contents.toString());

        try {
            //添加到mysql
            int i = cartoonContentService.setCartoonContent(cartoonContent);
            if(i==0){
                messageService.setMessage(new Message("mysql漫画内容阶段入库失败,chapterId:"+id.toString()+chapterId,new Date()));
            }

        }catch (DuplicateKeyException e){
        }catch (Exception e){
            messageService.setMessage(new Message("漫画内容阶段入库失败,chapterId:"+id.toString()+chapterId+" .Extends："+e,new Date()));
        }
        }
    }

